package com.easylinkin.linkappapi.weather.job;

import com.easylinkin.linkappapi.weather.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 天气数据定时任务
 */
@Component
@Slf4j
@ConditionalOnProperty(value = "weather.task.enabled", havingValue = "true", matchIfMissing = true)
public class WeatherTask {

    @Autowired
    private WeatherService weatherService;

    /**
     * 每小时执行一次天气数据获取任务
     * 执行时间：每小时的第10分钟
     */
    @Scheduled(cron = "0 10 * * * ?")
    public void fetchWeatherData() {
        log.info("定时任务：开始获取天气数据");
        try {
            weatherService.fetchAllTenantsWeatherData();
            log.info("定时任务：天气数据获取完成");
        } catch (Exception e) {
            log.error("定时任务：天气数据获取失败", e);
        }
    }

    /**
     * 手动触发天气数据获取
     */
    public void manualFetchWeatherData() {
        log.info("手动任务：开始获取天气数据");
        try {
            weatherService.fetchAllTenantsWeatherData();
            log.info("手动任务：天气数据获取完成");
        } catch (Exception e) {
            log.error("手动任务：天气数据获取失败", e);
        }
    }
} 